const express = require('express');
const userModel = require('../models/users');
const chatsModel = require('../models/chats');
const chatsService = require('../services/chats.service');
const chatsRoute = express.Router();

chatsRoute.post('/create', async (req, res) => {
  const params = req.body;
  const {status, msg, data} = chatsService.saveChatContentService(params);
  if (msg) {
    return res.status(status).json({msg});
  }
  return res.json(data);
});

chatsRoute.get('/list', async (req, res) => {
  const {sendId, receiveId} = req.query;
  if (!sendId || !receiveId) {
    return res.status(400).json({
      status: 400,
      msg: '发送者 id 或接受者 id 不存在'
    })
  }
  let unionId = [sendId, receiveId].sort().join('_');
  const result = await chatsModel.find({unionId});
  return res.json(result);
});


module.exports = chatsRoute;

